/*
package com.itheima.controller.backend.config;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SpringSecurityUserService implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        User user = userMapper.loadUserDetiles(username);

        if (user == null) {
            throw new UsernameNotFoundException("用户名不存在");
        }

        List<Role> roles = userMapper.loadRolesByUserId(user.getId());

        // 把角色列表转换成权限数组
        // 把角色转换成数组
        String[] roleKeywords = roles.stream()
                .map(Role::getKeyword)
                .collect(Collectors.toList())
                .toArray(new String[0]);
        List<GrantedAuthority> authorityList = AuthorityUtils.createAuthorityList(roleKeywords);
        // 设置用户的权限
        user.setAuthorities(authorityList);

        return user;
    }
}
*/
